
import CDATA from './constant'
import moment from 'moment'
// 星期的第一天是星期一 dow: 1
// 年份的第一周必须包含1月1日 (7 + 1 - 1)  doy: 7
moment.updateLocale('en', { week: { dow: 1, doy: 7 } })

export default moment

/**
 * 填报监控 显示函数
 * @param {*} row
 */
export function formatMonitor (row) {
  // console.log('row', row)
  if (CDATA.VALUE_DAY === row.cycleType) {
    return moment(row.fillBdate).format('M月D日H') + '点' + ' - ' + moment(row.fillEdate).format('M月D日H') + '点'
  }
  return moment(row.fillBdate).format('M月D日') + ' - ' + moment(row.fillEdate).format('M月D日') + ' 0点'
}

/**
 * 提报规则 显示函数
 * @param {*} row
 */
export function formatRule (row) {
  if (CDATA.VALUE_WEEK === row.typeCode) {
    return '' + row.unit + row.startTime + ' - ' + row.unit + row.endTime
  }
  return '' + row.startTime + row.unit + ' - ' + row.endTime + row.unit
}

/**
 * 获得默认日期
 */
export function initDate () {
  let day1 = moment(new Date()).add(-1, 'd') // 上一天
  let week1 = moment(new Date()).add(-1, 'w') // 上周
  let month1 = moment(new Date()).add(-1, 'M') // 上一月
  let quarter1 = moment(new Date()).add(-1, 'Q') // 上季度
  let year1 = moment(new Date()).add(-1, 'y') // 上一年
  let yearHalf1 = moment(new Date()).add(-6, 'M') // 上一半年
  let pickerVal = {
    day: day1.format('YYYY-MM-DD'),
    week: week1.toDate(),
    month: month1.format('YYYY-MM'),
    quarterly: '' + quarter1.year() + quarter1.quarter(),
    year: year1.format('YYYY'),
    'year_half': '' + yearHalf1.year() + (yearHalf1.month() <= 6 ? 'A' : 'P')
  }
  return pickerVal
}

// 以周一为每周的第一天
export function getWeekOfYear (today) {
  var firstDay = new Date(today.getFullYear(), 0, 1)
  var dayOfWeek = firstDay.getDay()
  var spendDay = 1
  if (dayOfWeek !== 0) {
    spendDay = 7 - dayOfWeek + 1
  }
  firstDay = new Date(today.getFullYear(), 0, 1 + spendDay)
  var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000)
  var result = Math.ceil(d / 7)
  return result + 1
}

/**
 * 处理格式
 */
export function delDate (pickerVal) {
  let _pickerVal = { ...pickerVal }
  if (_pickerVal.week) {
    let weekDate = _pickerVal.week
    let year = weekDate.getFullYear()
    let week = getWeekOfYear(weekDate)
    _pickerVal.week = '' + year + 'w' + week
  }
  return _pickerVal
}

/**
 * 读取 object 的所有值的数组
 * @param {*} object
 */
export function getObjectValues (object) {
  var values = []
  for (var property in object) {
    values.push(object[property])
  }
  return values
}
